﻿/*****************************************************************
// Copyright (C)
// All rights reserved.
// 
// Author:       WYQ
// Create Date:  2012-8-18 15:15:59
// Usage: 持久化数据库仓库
*****************************************************************/
using System;
using System.Linq;
using System.Linq.Expressions;

namespace Portal.Ef.Repository
{

   
    public class  CandidatesRepository : Repository<Candidates, Candidates, TbCandidates>, ICandidatesRepository<Candidates>
    {
        public override Candidates CreateEntity()
        {
            return new Candidates { };
        }
        public override IQueryable<Candidates> Get()
        {
            return MapQueryable(Entities.TbCandidates);
        }
        public override long Count()
        {
            return MapQueryable(Entities.TbCandidates).Count();
        }
        public override IQueryable<Candidates> Get(Expression<Func<Candidates, bool>> expression)
        {
            return MapQueryable(Entities.TbCandidates).Where(expression);
        }
        public override IQueryable<Candidates> GetSort(string sort = "Id", string order = "DESC")
        {
            Check.Argument.IsNotNull(sort, "排序字段");
            Check.Argument.IsNotNull(order, "排序条件");
            return LinqHelper.DataSorting(MapQueryable(Entities.TbCandidates), sort, order);
        }
        public override IQueryable<Candidates> GetSort(Expression<Func<Candidates, bool>> expression, string sort = "Id", string order = "DESC")
        {
            Check.Argument.IsNotNull(expression, "查询条件");
            Check.Argument.IsNotNull(sort, "排序字段");
            Check.Argument.IsNotNull(order, "排序条件");
            return LinqHelper.DataSorting(MapQueryable(Entities.TbCandidates).Where(expression), sort, order);
        }
        public override void Add(Candidates t)
        {
            Entities.AddToTbCandidates(ReverseMapModel(t));
        }
        public override void Update(Candidates t)
        {
            throw new NotImplementedException();
        }
        public override void AddOrUpdate(Candidates t)
        {
            if (Entities.TbCandidates.Any(o => o.CoCandidatesId == t.Id))
            {

            }
            else
            {
                Entities.AddToTbCandidates(ReverseMapModel(t));
            }
        }
        public override void Delete(Candidates t)
        {
            Entities.TbCandidates.Where(o => o.CoCandidatesId == t.Id).ForEach(o => Entities.DeleteObject(o));
        }
        public override void Delete(Expression<Func<Candidates, bool>> expression)
        {
            MapQueryable(Entities.TbCandidates).Where(expression).ForEach(o => Entities.DeleteObject(o));
        }
        public override void Save()
        {
            Entities.SaveChanges();
        }
       
        public JqEasyuiDataGrid<Candidates> JqEasyuiDataGrid(string code, string name, string categoryId, string sort = "CreateTime", string order = "Desc", int page = 10, int index = 1)
        {
            return new JqEasyuiDataGrid<Candidates>();
        }
      
        public override sealed IQueryable<Candidates> MapQueryable(IQueryable<TbCandidates> queryable)
        {
            return queryable.Select(o => new Candidates 
            {
         
	           Id=o.CoCandidatesId,
	           Name = o.CoName 
,
	           PostName = o.CoPostName 
,
	           Sex = o.CoSex 
,
	           DateBirth = o.CoDateBirth 
,
	           MaritalStatus = o.CoMaritalStatus 
,
	           ContactPhone = o.CoContactPhone 
,
	           ContactAddress = o.CoContactAddress 
,
	           Email = o.CoEmail 
,
	           Resume = o.CoResume 
,
	           Code = o.CoCode 
,
	           Culture = o.CoCulture 
,
	           CreateTime = o.CoCreateTime??DateTime.Now 
,
	           IsDelete = o.CoIsDelete??false 
,
	           Version = o.CoVersion??0 
,
	           IsClosed = o.CoIsClosed??false 
,
	           IsRead = o.CoIsRead??false 

            
            });
        }
      
        public override sealed IQueryable<TbCandidates> ReverseMapQueryable(IQueryable<Candidates> queryable)
        {
            return queryable.Select(o => new TbCandidates 
            { 
        
	           CoCandidatesId=o.Id,
	           CoName = o.Name
,
	           CoPostName = o.PostName
,
	           CoSex = o.Sex
,
	           CoDateBirth = o.DateBirth
,
	           CoMaritalStatus = o.MaritalStatus
,
	           CoContactPhone = o.ContactPhone
,
	           CoContactAddress = o.ContactAddress
,
	           CoEmail = o.Email
,
	           CoResume = o.Resume
,
	           CoCode = o.Code
,
	           CoCulture = o.Culture
,
	           CoCreateTime = o.CreateTime
,
	           CoIsDelete = o.IsDelete
,
	           CoVersion = o.Version
,
	           CoIsClosed = o.IsClosed
,
	           CoIsRead = o.IsRead

  
            
            });
        }
      
        public override sealed Candidates MapModel(TbCandidates model)
        {
            return new Candidates {
	           Id=model.CoCandidatesId,
	           Name = model.CoName
,
	           PostName = model.CoPostName
,
	           Sex = model.CoSex
,
	           DateBirth = model.CoDateBirth
,
	           MaritalStatus = model.CoMaritalStatus
,
	           ContactPhone = model.CoContactPhone
,
	           ContactAddress = model.CoContactAddress
,
	           Email = model.CoEmail
,
	           Resume = model.CoResume
,
	           Code = model.CoCode
,
	           Culture = model.CoCulture
,
	           CreateTime = model.CoCreateTime??DateTime.Now
,
	           IsDelete = model.CoIsDelete??false
,
	           Version = model.CoVersion??0
,
	           IsClosed = model.CoIsClosed??false
,
	           IsRead = model.CoIsRead??false
   
            };
        }
        
        public override sealed TbCandidates ReverseMapModel(Candidates model)
        {
            return new TbCandidates { 
	           CoCandidatesId=model.Id,
	           CoName = model.Name
	        
,
	           CoPostName = model.PostName
	        
,
	           CoSex = model.Sex
	        
,
	           CoDateBirth = model.DateBirth
	        
,
	           CoMaritalStatus = model.MaritalStatus
	        
,
	           CoContactPhone = model.ContactPhone
	        
,
	           CoContactAddress = model.ContactAddress
	        
,
	           CoEmail = model.Email
	        
,
	           CoResume = model.Resume
	        
,
	           CoCode = model.Code
	        
,
	           CoCulture = model.Culture
	        
,
	           CoCreateTime = model.CreateTime
	        
,
	           CoIsDelete = model.IsDelete
	        
,
	           CoVersion = model.Version
	        
,
	           CoIsClosed = model.IsClosed
	        
,
	           CoIsRead = model.IsRead
	        
,
	      
	        
            
            
            };
        }


    }
}



